﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InterfazOperacion;
using DivisionException;

namespace Proyecto_Tres
{
    public partial class CalculadoraSimple : Form
    {
        private Cliente PlugsHost;

        public CalculadoraSimple()
        {
            InitializeComponent();
            PlugsHost = new Cliente();
            PlugsHost.CargarPlugins();
            foreach (Operacion Plug in PlugsHost.getPlugins())
            {
                comboBox1.Items.Add(Plug.getSimbolo());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            String Text1 = operando1.Text;
            String Text2 = operando2.Text;
            double op1 = 0;
            double op2 = 0;
            Boolean error = false;
            try
            {
                op1 = Convert.ToDouble(Text1);
                op2 = Convert.ToDouble(Text2);
            }
            catch (FormatException)
            {
                MessageBox.Show("ERROR: Operando invalido");
                operando1.Text = "";
                operando2.Text = "";
                label1.Text = "";
                error = true;
            }
            catch (OverflowException)
            {
                MessageBox.Show("ERROR: Se ingreso un valor muy grande");
                operando1.Text = "";
                operando2.Text = "";
                label1.Text = "";
                error = true;
            }
            if (!error)
            {
                if (comboBox1.SelectedItem == null)
                {
                    MessageBox.Show("Por favor, seleccione una operacion ");
                    operando1.Text = "";
                    operando2.Text = "";
                    label1.Text = "";
                }
                else
                {
                    char elOperador = (char)comboBox1.SelectedItem;
                    foreach (Operacion Plug in PlugsHost.getPlugins())
                    {
                        if (Plug.getSimbolo() == elOperador)
                        {
                            try
                            {
                                label1.Text = Plug.operar(op1, op2) + "";
                            }
                            catch (DivisionException.DivisionPorCeroException exc)
                            {
                                MessageBox.Show(exc.Message);
                                operando1.Text = "";
                                operando2.Text = "";
                                label1.Text = "";
                            }
                            catch (DivisionException.PotenciaDeCeroException exc)
                            {
                                MessageBox.Show(exc.Message);
                                operando1.Text = "";
                                operando2.Text = "";
                                label1.Text = "";
                            }              
                        }
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PlugsHost.CargarPlugins();
            comboBox1.Items.Clear();
            operando1.Text = "";
            operando2.Text = "";
            label1.Text = "<resultado>";
            operador.Text = "<Op>";

            List<Operacion> losPlugins = PlugsHost.getPlugins();
            foreach (Operacion Plug in losPlugins)
            {
                comboBox1.Items.Add(Plug.getSimbolo());
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            operador.Text = "   "+(Char)comboBox1.SelectedItem;
        }










    }
}
